<div class="dialog dialog-fenpeify hidden">
    <div class="box" style="height: 380px;">
        <div class="title">
            <h4 class="fl">办理入住手续对话框</h4>
            <span class="fr"><a href="#" onclick="closeDialog();">关闭</a></span>
        </div>
        <table class="form-tab">
            <tr>
                <th>合同编号：</th>
                <td>
                    <input name="contact-id" type="hidden" value="" />
                    <input name="contact" value="" type="text" />
                    <label>
                        <a name="con-create" onclick="action_contact()" href="javascript:void(0);">创建合同</a>
                        <a name="con-detail" class="hidden" onclick="action_ContactDetail()" href="javascript:void(0);">查看合同</a>
                    </label>
                </td>
            </tr>
            <tr>
                <th width="110">分配房源：</th>
                <td>
                    <input disabled="disabled" name="house" type="text" />
                </td>
            </tr>
            <tr>
                <th>应缴租金：</th>
                <td>
                    <input name="yjzj" type="text" disabled="disabled" />
                    <label>元</label>
                </td>
            </tr>
            <tr>
                <th>应缴押金：</th>
                <td>
                    <input name="yjyj" value="" type="text" disabled="disabled" />
                    <label>元</label>
                </td>
            </tr>
            <tr>
                <th>合计应缴金额：</th>
                <td>
                    <input name="hjje" type="text" disabled="disabled" />
                    <label>元</label>
                </td>
            </tr>
            <tr>
                <th>办理日期：</th>
                <td>
                    <input name="ctime" type="text" value="{$ctime|default=time()|date='Y/m/d',###}" />
                </td>
            </tr>
            <tr>
                <td colspan="2" style="text-align: center;line-height:40px;">
                    <button class="btn btn-highlight" onclick="action_submit();">提交</button>
                    <span>或</span>
                    <a href="#" onclick="closeDialog();" class="linkBtn">放弃</a>
                </td>
            </tr>
        </table>
    </div>
</div>
<div class="dialog dialog-contact hidden">
    <div class="box" style="margin-top:40px;height:520px;width:800px;">
        <div class="title">
            <h4 class="fl">创建合同对话框</h4>
            <span class="fr"><a href="#" onclick="action_CloseContactDlg();">关闭</a></span>
        </div>
        <div class="form-box2" style="margin: 0;">
            <div>
                <table class="form">
                    <tr>
                        <th width="15%">业务编号<span class="hint">*</span></th>
                        <td width="35%"><input name="ywno" type="text" value="{$contact.ywno}" /> </td>
                        <th width="15%">合同类型<span class="hint">*</span></th>
                        <td><input class="disabled" name="type" type="text" value="{$contact.type}" /></td>
                    </tr>
                    <tr>
                        <th>房源地址<span class="hint">*</span></th>
                        <td>
                            <input class="hidden" name="community" type="text" value="{$contact.community}" />
                            <input class="hidden" name="fwbh" type="text" value="{$contact.fwbh}" />
                            <input class="disabled" name="fwdz" type="text" value="{$contact.fwdz}" />
                        </td>
                        <th>房源面积<span class="hint">*</span></th>
                        <td><input class="disabled" name="size" type="text" value="{$contact.size}" /> </td>
                    </tr>
                    <tr>
                        <th>标准月租金(元)<span class="hint">*</span></th>
                        <td><input class="disabled" name="zj" type="text" value="{$contact.zj}" /> </td>
                        <th>市场月租金(元)<span class="hint">*</span></th>
                        <td><input class="disabled" name="mzj" type="text" value="{$contact.mzj}" /> </td>
                    </tr>
                    <tr>
                        <th>甲方<span class="hint">*</span></th>
                        <td><input class="disabled" name="jiafang" type="text" value="{$contact.jiafang|default=C('CONTACT_JIAFANG')}" /> </td>
                        <th>乙方<span class="hint">*</span></th>
                        <td><input class="disabled" name="yifang" type="text" value="{$contact.yifang}" /> </td>
                    </tr>
                    <tr>
                        <th>合同开始日期<span class="hint">*</span></th>
                        <td><input name="ksrq" type="text" style="border:1px solid red;" value="{$contact.ksrq|default=$contactDate['start']}" /> </td>
                        <th>合同结束日期<span class="hint">*</span></th>
                        <td><input class="disabled" name="jsrq" style="border:1px solid red;" type="text" value="{$contact.jsrq|default=$contactDate['end']}" /> </td>
                    </tr>
                    <tr>
                        <th>押金(元)<span class="hint">*</span></th>
                        <td><input name="yj" type="text" value="{$contact.yj|default=C('CONTACT_YAJIN')}" /> </td>
                        <th>低保人口<span class="hint">*</span></th>
                        <td><input class="disabled" name="dibaork" type="text" value="{$contact.dibaork|default=0}" /> </td>
                    </tr>
                    <tr>
                        <th>减免类型<span class="hint">*</span></th>
                        <td>
                            <select name="derate_type" style="width: 215px;">
                                <volist name="derate_type" id="vo" key="index">
                                    <option value="{$vo[0]}" derate="{$vo[1]}">{$vo[0]}</option>
                                </volist>
                            </select>
                        </td>
                        <th>减免金额(元)<span class="hint">*</span></th>
                        <td><input class="disabled" name="derate_money" type="text" value="{$contact.derate_money|default='0.00'}" /> </td>
                    </tr>
                    <tr>
                        <th>应缴租金<span class="hint">*</span></th>
                        <td><input class="disabled" name="total2" type="text" value="{$contact.total2}" /> </td>
                        <th>减免后应缴租金<span class="hint">*</span></th>
                        <td><input class="disabled" name="total" type="text" value="{$contact.total}" /> </td>
                    </tr>
                    <tr>
                        <th>甲方法定代表人<span class="hint">*</span></th>
                        <td><input name="jffr" type="text" value="{$contact.jffr|default=C('CONTACT_FAREN')}" /> </td>
                        <th>乙方身份证身份证<span class="hint">*</span></th>
                        <td><input class="disabled" name="yfsfz" type="text" value="{$contact.yfsfz}" /> </td>
                    </tr>
                    <tr>
                        <th>甲方经办人<span class="hint">*</span></th>
                        <td><input name="jfjbr" type="text" value="{$contact.jfjbr|default=C('CONTACT_JINBAN')}" /> </td>
                        <th>乙方联系地址</th>
                        <td><input name="yfdz" type="text" value="{$contact.yfdz}" /> </td>
                    </tr>
                    <tr>
                        <th>甲方联系电话<span class="hint">*</span></th>
                        <td><input name="jfdh" type="text" value="{$contact.jfdh|default=C('CONTACT_PHONE')}" /> </td>
                        <th>乙方电话<span class="hint">*</span></th>
                        <td><input name="yfdh" type="text" value="{$contact.yfdh}" /> </td>
                    </tr>
                    <tr style="vertical-align: top;">
                        <th>签订日期<span class="hint">*</span></th>
                        <td><input name="ctime" type="text" value="{$contact.ctime|default=time()|date='Y/m/d',###}" /> </td>
                    </tr>
                    <tr>
                        <td colspan="4">
                            <input type="hidden" name="default-ywno" value="{$lottery.no}" />
                            <button class="btn btn-highlight" onclick="action_CreateContact();">提交</button>
                            <span>或</span>
                            <a href="#" onclick="action_CloseContactDlg();" class="linkBtn">放弃</a>
                        </td>
                    </tr>
                </table>
            </div>
        </div>
    </div>
</div>
<link type="text/css" href="__JS__/datetimepicker/pikaday.css" rel="stylesheet">
<script src="__JS__/datetimepicker/pikaday.min.js" type="text/javascript"></script>
<link type="text/css" href="__JS__/autocomplete2/src/jquery.autocomplete.min.css" rel="stylesheet">
<script src="__JS__/autocomplete2/src/jquery.autocomplete.js" type="text/javascript"></script>
<script>

    $(document).ready(function(){
        // 自动填写默认 业务申请编号
        $("input[name=ywno]").val($("input[name=default-ywno]").val());

        $("select[name=derate_type]").change(function(){
            var total = parseFloat($("input[name=total2]").val());
            var derate = parseFloat($(this).find("option:selected").attr("derate"));
            var derate_money = total * derate;
            $("input[name=total]").val((total-derate_money).toFixed(2));
            $("input[name=derate_money]").val(derate_money.toFixed(2));
        });
    });

    //disable所有自动填充字段；
    $(".disabled").attr("disabled", true);

    function action_ContactDetail(){
        var conId = $("input[name=contact-id]").val();
        location.href = "/admin/contact/detail?id=" + conId;
    }

    function action_contact(){
        $(".dialog-contact").show();
        $("input[name=ywno]").focus();
    }

    function action_CloseContactDlg(){
        $(".dialog-contact").hide();
    }

    // 提交 创建合同 对话框内容
    function action_CreateContact(){
        var data = {
            "derate_type": $("select[name=derate_type]").val(),
            "derate_money": $("input[name=derate_money]").val(),
            "ywno": $("input[name=ywno]").val(),
            "community": $("input[name=community]").val(),
            "type": $("input[name=type]").val(),
            "dibaork": $("input[name=dibaork]").val(),
            "jiafang": $("input[name=jiafang]").val(),
            "yifang": $("input[name=yifang]").val(),
            "fwbh": $("input[name=fwbh]").val(),
            "fwdz": $("input[name=fwdz]").val(),
            "size": $("input[name=size]").val(),
            "zj": $("input[name=zj]").val(),
            "mzj": $("input[name=mzj]").val(),
            "total": $("input[name=total]").val(),
            "total2": $("input[name=total2]").val(),
            "yj": $("input[name=yj]").val(),
            "ksrq": $("input[name=ksrq]").val(),
            "jsrq": $("input[name=jsrq]").val(),
            "jffr": $("input[name=jffr]").val(),
            "jfjbr": $("input[name=jfjbr]").val(),
            "jfdz": $("input[name=jfdz]").val(),
            "jfdh": $("input[name=jfdh]").val(),
            "yfsfz": $("input[name=yfsfz]").val(),
            "yfdw": $("input[name=yfdw]").val(),
            "yfdz": $("input[name=yfdz]").val(),
            "yfdh": $("input[name=yfdh]").val(),
            "ctime": $("input[name=ctime]").val()
        };
        if(!data.ywno || !data.fwbh){
            alert("请填写有效的业务编号");
            $("input[name=ywno]").focus();
            return true;
        }
        if(parseFloat(data.size) <= 0){
            alert("房源面积输入有误，请检查房源:"+data.fwdz+"的房源面积是否设置正确");
            $("input[name=size]").focus();
            return true;
        }
        if(parseFloat(data.zj) <= 0){
            alert("标准月租金输入有误，请检查房源:"+data.fwdz+"的月租金是否设置正确");
            $("input[name=zj]").focus();
            return true;
        }
        if(parseFloat(data.mzj) <= 0){
            alert("市场月租金输入有误，请检查房源:"+data.fwdz+"的市场月租金是否设置正确");
            $("input[name=mzj]").focus();
            return true;
        }
        if(parseFloat(data.total2) <= 0){
            alert("应缴租金输入有误，请检查房源:"+data.fwdz+"的月租金是否设置正确");
            $("input[name=total2]").focus();
            return true;
        }
        $.ajax({
            url:"/admin/contact/ajax_CreateContact",
            data: data, type:"POST", dataType:"JSON",
            success: function(data){
                if(data.status){
                    action_CloseContactDlg();
                    $("a[name=con-create]").hide();
                    $("a[name=con-detail]").show();
                    $("input[name=contact]").val(data.info.no).focus();
                    $("input[name=contact-id]").val(data.info.dataId);
                }else{
                    console.log(data.info);
                    alert(data.info);
                }
            }
        });
    }

    $('input[name=ywno]').autocomplete({
        serviceUrl: '/admin/contact/ajax_SearchYaohaoByNo',
        extraParams: {ywno: $.trim($("input[name=ywno]").val())},//动态参数值
        paramName: "ywno",//自定义的参数名 必须和extraParams 参数名相同
        showNoSuggestionNotice: true,
        noSuggestionNotice: '对不起，没有匹配到待入住的业务编号',
        onSelect: function (suggestion) {
            $("input[name=type]").val(suggestion.data.type);
            $("input[name=community]").val(suggestion.data.community);
            $("input[name=fwbh]").val(suggestion.data.hno);
            $("input[name=size]").val(suggestion.data.size);
            $("input[name=zj]").val(suggestion.data.price);
            $("input[name=mzj]").val(suggestion.data.mprice);
            $("input[name=fwdz]").val(suggestion.data.addr);
            $("input[name=dibaork]").val(suggestion.data.dibaork);
            $("input[name=total]").val(suggestion.data.total.toFixed(2));
            $("input[name=total2]").val(suggestion.data.total.toFixed(2));
            $("input[name=yj]").val(suggestion.data.yajin);
            $("input[name=yifang]").val(suggestion.data.name);
            $("input[name=yfsfz]").val(suggestion.data.idno);
            $("input[name=yfdh]").val(suggestion.data.phone);
        }
    });

    var picker = new Pikaday(
            {
                field: document.getElementsByName('ksrq')[0],
                firstDay: 1,
                minDate: new Date('2015/01/01'),
                maxDate: new Date('2025/12/31'),
            });
    $("input[name=ksrq]").change(function(){
        reCalRental();
    });
    function reCalRental(){
        var date1 = $("input[name=ksrq]").val();
        var date2 = $("input[name=jsrq]").val();
        if(!isDate(date1)){
            alert("合同开始日期不是有效的日期格式");
            $(this).focus();
            return false;
        }
        if(!isDate(date2)){
            alert("合同结束日期不是有效的日期格式");
            $(this).focus();
            return false;
        }
        var yj = parseFloat($("input[name=yj]").val());
        var zj = parseFloat($("input[name=zj]").val());
        var size = parseFloat($("input[name=size]").val());
        var dibaork = parseInt($("input[name=dibaork]").val());
        var zujing = calZujin(date1, date2, zj, size, dibaork);
        $("input[name=total]").val(zujing.toFixed(2));
        var derate = parseFloat($("select[name=derate_type]").find("option:selected").attr("derate"));
        var derate_money = zujing * derate;
        $("input[name=total2]").val((zujing - derate_money).toFixed(2));
    }

    function dateRangeToFloat(date1, date2){
        var y1 = parseInt(date1.substr(0, 4));
        var y2 = parseInt(date2.substr(0, 4));
        var m1 = parseInt(date1.substr(5, 2));
        var m2 = parseInt(date2.substr(5, 2));
        var d1 = parseInt(date1.substr(8, 2));
        var d2 = parseInt(date2.substr(8, 2));
        if(y1!=y2){
            alert("日期错误，合同开始日期与结束日期需为同一年份");
            return false;
        }
        if(m1>m2){
            alert("日期错误，开始日期不能大于结束日期");
            return false;
        }
        if(m1==m2 && d1>=d2){
            alert("日期错误，开始日期不能大于结束日期");
            return false;
        }
        if(m2-m1==0){ //同年同月的情况
            return (y2-y1)*12 + Math.min((d2-d1+1)/30, 1);
        }else{
            return (y2-y1)*12 + Math.min(d2/30, 1) + m2-m1 - Math.min((d1-1)/30, 1);
        }
    }

    function calZujin(date1, date2, zujin, size, dibaork){
        var month = dateRangeToFloat(date1, date2);
        if(month!==false){
            if(dibaork*13>=size){ //低保人口享受面积大于房屋面积，则不要出房租
                return size*1.1*month;
            }
            //13平米每人内按1.1元每平米的价格计算
            var zj = (size-dibaork*13)*zujin/size + dibaork*13*1.1;
            return zj*month;
        }
    }

    function isDate(dateString){
        if(dateString.trim()=="")return true;
        var r=dateString.match(/^(\d{1,4})(\/)(\d{1,2})\2(\d{1,2})$/);
        if(r==null){
            alert("请输入格式正确的日期\n\r日期格式：yyyy/mm/dd\n\r例  如：2008/08/08\n\r");
            return false;
        }
        var d=new Date(r[1],r[3]-1,r[4]);
        var num = (d.getFullYear()==r[1]&&(d.getMonth()+1)==r[3]&&d.getDate()==r[4]);
        if(num==0){
            alert("请输入格式正确的日期\n\r日期格式：yyyy-mm-dd\n\r例  如：2008-08-08\n\r");
        }
        return (num!=0);
    }

    /* 签订合同 */
    function action_submit() {
        var id = $("input[name=id]").val();
        var contact = $("input[name=contact]").val();
        var ctime = $("input[name=ctime]").val();
        if(!contact){
            alert("请填写合同编号");
            $("input[name=contact]").focus();
            return false;
        }
        if(!ctime){
            alert("请填写分配日期");
            $("input[name=ctime]").focus();
            return false;
        }
        $.ajax({
            url:"/admin/lottery/ajax_fenpeify",
            data: {id:id, contact:contact,ctime:ctime}, type:"POST", dataType:"JSON",
            success: function(data){
                if(data.status){
                    location.href = '/admin/admin/ok?msg='+data.info;
                }else{
                    console.log(data.info);
                    alert(data.info);
                }
            }
        });
    }

    /* 新合同 */
    $('input[name=contact]').autocomplete({
        serviceUrl: '/admin/lottery/ajax_SearchContactByNo',
        extraParams: {no:$.trim($("input[name=contact]").val())},//动态参数值
        paramName: "no",//自定义的参数名 必须和extraParams 参数名相同
        onSelect: function (suggestion) {
            console.log(suggestion);
            var zujin = parseFloat(suggestion.data.total);
            if(suggestion.data.derate_money>0){
                $('input[name=yjzj]').val( zujin + '（已减免'+ suggestion.data.derate_money + '）' );
            }else{
                $('input[name=yjzj]').val( zujin  );
            }
            var yajin = parseFloat(suggestion.data.yj);
            $('input[name=yjyj]').val( yajin );
            var hjje= zujin + yajin;
            $('input[name=hjje]').val( hjje.toFixed(2) );
            $('input[name=house]').val( suggestion.data.fwdz );
        }
    });

    /**
     * 打开办理入住手续对话框
     */
    function action_fenpeify() {

        $(".dialog-fenpeify").show();
    }

    /**
     * 关闭对话框
     */
    function closeDialog() {
        $(".dialog").hide();
    }
</script>
